﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Globalization;
/*
Copyright 2010 Mirosław M. Mączka

This file is part of Mp3Cleaner.

    Mp3Cleaner is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace Mp3Edit
{
    public sealed class CaseUtils
    {
        //
        private static TextInfo ti = null;
        //
        public enum CASE { SentenceCase, lowercase, UPPERCASE, CapitalizeEachWord, tOGGLEcASE, SwapLastFirst};
        //
        static CaseUtils()
        {
            //
            ti = new CultureInfo("en-US", false).TextInfo;
            //
        }
        //
        //--------------------------------------
        public static string ChangeCase(CASE c, string s)
        {
            switch (c)
            {
                case CASE.SentenceCase:
                    s = ToSentence(s);
                    break;
                case CASE.lowercase:
                    s = ti.ToLower(s);
                    break;
                case CASE.UPPERCASE:
                    s = ti.ToUpper(s);
                    break;
                case CASE.CapitalizeEachWord:
                    s=ti.ToTitleCase(ti.ToLower(s));
                    break;
                case CASE.tOGGLEcASE:
                    s=ToToggle(s);
                    break;
                case CASE.SwapLastFirst:
                    s = swapLastFirstName(s);
                    break;
            }
            return s;
        }
        //--------------------------------------
        public static string ToSentence(string src)
        {
            bool first = true;
            string s="";
            foreach (char c in ti.ToLower(src))
            {
                if(first && !Char.IsWhiteSpace(c))
                {
                    first=false;
                    s += Char.ToUpper(c);
                }
                else
                {
                    s+=c;
                }
            }
            //
            return s;
        }
        //--------------------------------------
        public static string ToToggle(string src)
        {
            string s = "";
            foreach (char c in src)
            {
                if (Char.IsLower(c))
                {
                    s += Char.ToUpper(c);
                }
                else if (Char.IsUpper(c))
                {
                    s += Char.ToLower(c);
                }
                else
                {
                    s += c;
                }
            }
            //
            return s;
        }
        //--------------------------------------
        public static string ToCamelIfNeeded(string src)
        {
            if ((src == ti.ToLower(src)) || (src == ti.ToUpper(src)))
            {

                src = ti.ToTitleCase(ti.ToLower(src));
            }
            return src;
        }
        //--------------------------------------
        public static string ToNothing(string s)
        {
            return s;
        }
        //--------------------------------------
        public static string ToLower(string s)
        {
            return ti.ToLower(s);
        }
        //--------------------------------------
        public static string ToUpper(string s)
        {
            return ti.ToUpper(s);
        }
        //--------------------------------------
        public static string swapLastFirstName(string s)
        {
            string[] ss = s.Split(',');
            if (2 != ss.Length) return s;
            //
            ss[0] = ss[0].Trim();
            ss[1] = ss[1].Trim();
            //
            if ("" == ss[0] || "" == ss[1]) return s;
            //
            return ss[1] + " " + ss[0];
        }
        //
    }//class
}//namespace